from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from page_model.base_page import Base


class SendEmail(Base):

    def login(self, name, pwd):
        """
        登录的方法
        :param name: 登录的用户名
        :param pwd: 登录的密码
        :return: 不返回值，进入到界面内
        """
        #  切入内嵌页中
        self.frame((By.XPATH, '//*[@id="loginDiv"]/iframe'))
        #  输入用户名
        self.send_keys((By.XPATH, '//*[@name="email"]'), name)
        #  输入密码
        self.send_keys((By.XPATH, '//*[@name="password"]'), pwd)
        #  点击登录
        self.click((By.XPATH, '//*[@id="dologin"]'))

    def send_email(self, name, zhuti, wenjian, value):
        """
        发送一个邮件的方法
        :param name: 发送给哪个账户
        :param zhuti: 发送的主题
        :param wenjian: 发送的文件
        :param value: 发送的主体内容
        :return: 返回一个列表，其中包括发送成功文本和发送时间
        """
        #  点击发邮件
        self.click((By.XPATH, '//*[@id="_mail_component_74_74"]'))
        #  输入收件人
        self.send_keys((By.XPATH, '//*[@title="发给多人时地址请以分号隔开"]/div/input'), name)
        #  输入主题
        self.send_keys((By.XPATH, '//*[@aria-label="邮件主题输入框，请输入邮件主题"]/input'), zhuti)
        #  上传附件
        self.send_keys((By.XPATH,
                        '/html/body/div[2]/div[1]/div[2]/div[1]/section/header/div[3]/div[1]/div[2]/input'), wenjian)
        #  等待上传完成
        locator = (By.XPATH, '//*[@class="nui-txt-suc"]')
        WebDriverWait(self.driver, 1000, 1).until(EC.text_to_be_present_in_element(locator, '上传完成'))
        #  切入内嵌页面
        self.frame((By.XPATH, '/html/body/div[2]/div[1]/div/div[1]/section/section/div/div[1]/div[1]/div[2]/iframe'))
        #  输入邮件内容
        self.send_keys((By.XPATH, '/html/body'), value)
        #  点击发送
        self.parent_frame()
        self.click((By.XPATH, '//*[@tabindex="1"]'))
        #  获取发送成功文本
        a = self.get_text((By.XPATH, '//*[@class="tK1"]'))
        #  点击查看已发送邮件
        self.click((By.LINK_TEXT, '查看已发邮件'))
        #  获取时间
        b = self.find_elements((By.XPATH, '//*[@class="hX0"]'))
        c = b[2].text
        self.driver.quit()
        ls = [a, c]
        return ls
